import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttermodule/clients/clients.dart';
import 'package:fluttermodule/provide/enterprise_provide.dart';
import 'package:fluttermodule/widget/ProductEntity.dart';
import 'package:provide/provide.dart';

import 'enterprise_model.dart';

class Enterprise extends StatefulWidget {
  @override
  _EnterpriseState createState() {
    return _EnterpriseState();
  }
}

class _EnterpriseState extends State<Enterprise> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "SuperApp",
      home: Scaffold(
//        backgroundColor: Color(0xFF4393E3),
        body: FutureBuilder(
          future: _getEnterpricsInfo(context),
          builder: (context, snapshot) {
            var result =
                Provide.value<EnterpriseInfoProvide>(context).enterpriseInfo;
            if (result != null) {
              return
                Container(
                  color:Color(0xFF4393E3),
                  child:ListView(
                    shrinkWrap: true,

                    children: <Widget>[

                      FirstArea(),
                      InternalIndexArea(index: result.jxindext),
                      InfosArea(infos: result.msg),
                      Container(
                        child: Divider(
                          color: Color(0xffF6F6F6),
                          height: 20.0,
                          thickness: 20.0,
                        ),
                      ),
                      MyDataArea(result: result),
                    ],
                  ),
                );

            } else {
              return Text("");
            }
            ;
          },
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  //获取当前事业模块信息
  Future _getEnterpricsInfo(BuildContext context) async {
    try {
      await Provide.value<EnterpriseInfoProvide>(context).getEnterpriseInfo();
    } catch (e) {
      print(e);
    }
  }
}

class MyDataArea extends StatelessWidget {
  var infos = new List<Highlevers>();
  EnterpriseInfo result;

  MyDataArea({Key key, this.result}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    infos = result.highlevers;
    return Container(
//      color: Colors.white,
      padding:
          const EdgeInsets.only(top: 20, left: 15, right: 15.0, bottom: 20.0),
      constraints: BoxConstraints(
          minWidth: double.infinity, //宽度尽可能大
          minHeight: 340.0 //最小高度为50

          ), //卡片大小
      decoration: BoxDecoration(
        //背景装饰
        color: Colors.white,
      ),
      child: Column(
        children: <Widget>[
          Container(
            margin: const EdgeInsets.only(left: 20.0, right: 25.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Column(
                  children: <Widget>[
                    Text(
                      result.amyfriend,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 28,
                          fontWeight: FontWeight.w500),
                    ),
                    Text(
                      "新增安利之友",
                      style:
                          TextStyle(color: Color(0xff666666), fontSize: 13.0),
                    )
                  ],
                ),
                Column(
                  children: <Widget>[
                    Text(result.newCard,
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 28,
                            fontWeight: FontWeight.w500)),
                    Text(
                      "新增办卡",
                      style:
                          TextStyle(color: Color(0xff666666), fontSize: 13.0),
                    )
                  ],
                ),
                Column(
                  children: <Widget>[
                    Text(result.needContinueCard,
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 28,
                            fontWeight: FontWeight.w500)),
                    Text(
                      "待续卡",
                      style:
                          TextStyle(color: Color(0xff666666), fontSize: 13.0),
                    )
                  ],
                ),
              ],
            ),
          ),
          Container(
            //描述信息栏目，右键查看全部
            margin: const EdgeInsets.only(top: 20.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Expanded(
                  child: Text(
                    "绩优人员国内进修指数",
                    style: TextStyle(
                        color: Color(0xff333333),
                        fontSize: 16.0,
                        fontWeight: FontWeight.w600),
                  ),
                ),
                Text(
                  "查看全部",
                  style: TextStyle(
                    color: Color(0xff999999),
                    fontSize: 13.0,
                  ),
                ),
                Icon(
                  Icons.keyboard_arrow_right,
                  color: Color(0xff999999),
                  size: 20,
                )
              ],
            ),
          ),
          Container(
            //用户列表list
            margin: const EdgeInsets.only(top: 10, bottom: 20),

            child: ListView.builder(
                shrinkWrap: true,
                itemCount: result.highlevers.length,
                padding: const EdgeInsets.only(top: 10.0),
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                      padding: EdgeInsets.only(bottom: 15),
                      child: Row(
                        children: <Widget>[
                          CircleAvatar(
                            backgroundImage:
                                NetworkImage(infos[index].headImae),
                          ),
                          Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Container(
                                  child: Text(
                                    infos[index].name,
                                    style: TextStyle(
                                        color: Color(0xff333333),
                                        fontSize: 14.0,
                                        fontWeight: FontWeight.w400),
                                  ),
                                  margin: const EdgeInsets.only(
                                      bottom: 5, left: 15),
                                ),
                                Container(
                                  width: 200,
                                  height: 5.0,
                                  margin: const EdgeInsets.only(left: 15),
                                  child: LinearProgressIndicator(
                                    value: double.parse(infos[index].index),
                                    valueColor:
                                        new AlwaysStoppedAnimation<Color>(
                                            Color(0xff6DC1FE)),
                                    backgroundColor: Color(0xffececec),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(
                            margin: const EdgeInsets.only(top: 20, left: 20),
                            child: Text(infos[index].number),
                          )
                        ],
                      ));
                }),
          ),
        ],
      ),
    );
  }
}

class InfosArea extends StatelessWidget {
  final String infos;

  InfosArea({Key key, this.infos}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.only(
          top: 10.0, left: 20.0, bottom: 10.0, right: 10.0),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Icon(Icons.volume_up, color: Color(0xff77B8F8)),
              Expanded(
                child: Container(
                  margin: const EdgeInsets.only(left: 10.0, right: 25.0),
                  child: Text(
                    infos,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: Color(0xff666666), fontSize: 14.0),
                  ),
                ),
              ),
              Icon(
                Icons.keyboard_arrow_right,
                color: Color(0xff666666),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class InternalIndexArea extends StatelessWidget {
  final String index;

  InternalIndexArea({Key key, this.index}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(10.0),
      padding: const EdgeInsets.only(
          top: 5.0, right: 15.0, left: 15.0, bottom: 20.0),
      decoration: BoxDecoration(
        color: Color(0xff3076c3),
        borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
      ),
      child: Column(
        children: <Widget>[
          Container(
            alignment: Alignment.topLeft,
            margin: const EdgeInsets.only(top: 10.0),
            child: Text(
              "国内进修指数",
              style: TextStyle(fontSize: 13.0, color: Colors.white),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(top: 5.0, bottom: 15.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Stack(
                  alignment: Alignment.topLeft,
                  fit: StackFit.loose,
                  overflow: Overflow.visible,
                  children: <Widget>[
                    Container(
                      child: Text(
                        index,
                        style: TextStyle(
                            fontSize: 30.00,
                            color: Colors.white,
                            fontWeight: FontWeight.w500),
                      ),
                    ),
                    Positioned(
                      top: 1.0,
                      right: -25.0,
                      child: Icon(
                        Icons.star,
                        color: Colors.red,
                        size: 20.0,
                      ),
                    ),
                  ],
                ),
                Container(
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    border:
                        new Border.all(width: 1.0, color: Color(0xff568bcb)),
                    color: Color(0xff568bcb),
                    borderRadius:
                        new BorderRadius.all(new Radius.circular(45.0)),
                  ),
                  padding: const EdgeInsets.fromLTRB(10, 4, 10, 4),
                  child: Text(
                    "我的业绩",
                    style: TextStyle(fontSize: 14.0, color: Colors.white),
                  ),
                )
              ],
            ),
          ),
          Divider(
            color: Colors.white,
            thickness: 0.25,
          ),
          Container(
            margin: const EdgeInsets.only(left: 20.0, right: 20.0, top: 10.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Stack(
                  overflow: Overflow.visible,
                  children: <Widget>[
                    Text(
                      "我的微购",
                      style: TextStyle(color: Colors.white, fontSize: 14.0),
                    ),
                    Positioned(
                      top: 1,
                      right: -4,
                      child: Icon(
                        Icons.fiber_manual_record,
                        color: Colors.red,
                        size: 5.0,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  width: 0.25,
                  height: 12,
                  child: DecoratedBox(
                    decoration: BoxDecoration(color: Colors.white),
                  ),
                ),
                Stack(
                  overflow: Overflow.visible,
                  children: <Widget>[
                    Text(
                      "我的收入",
                      style: TextStyle(color: Colors.white, fontSize: 14.0),
                    ),
                    Positioned(
                      top: 1,
                      right: -4,
                      child: Icon(
                        Icons.fiber_manual_record,
                        color: Colors.red,
                        size: 5.0,
                      ),
                    )
                  ],
                ),
                SizedBox(
                  width: 0.25,
                  height: 12,
                  child: DecoratedBox(
                    decoration: BoxDecoration(color: Colors.white),
                  ),
                ),
                Text(
                  "奖励计划",
                  style: TextStyle(color: Colors.white, fontSize: 14.0),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class FirstArea extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 50.0, left: 10.0, right: 10.0),
      child: Row(
        children: <Widget>[
          Expanded(
              child: Text(
            "事业",
            style: TextStyle(fontSize: 30.0, color: Color(0xffffffff)),
          )),
          Icon(
            Icons.account_box,
            color: Colors.white,
          ),
          Container(
              width: 70,
              child: FlatButton(
                padding: const EdgeInsets.only(top: 1),
                child: Text(
                  "顾客管理",
                  style: TextStyle(color: Colors.white, fontSize: 17.0),
                ),
                onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return CustomerList();
                  }));
                },
              ))
        ],
      ),
    );
  }
}
